//backend
//植株贴士growtip
const router = require('koa-router')()

router.prefix('/growtips')

//页面加载完成时获取数据库里全部数据
router.post('/getgrowtipsbackend', async (ctx, next) => {
    let result = await ctx.db.EXCUTE("SELECT n.*, (SELECT cropname FROM tags ni WHERE n.cropsid=ni.cropsid) AS comings FROM growtip n")

    ctx.body = {
        code: 200,
        data: result
    }
})

//编辑项 更新
router.post('/getname', async (ctx, next) => {
    let result = await ctx.db.EXCUTE('SELECT * FROM tags');

    console.log(result);
    ctx.body = {
        code: 200,
        data: result
    }
})
//编辑项 更新
router.post('/modifygrowtipsbackend', async (ctx, next) => {
    let cropsid = ctx.request.body.cropsid;
    let tipid = ctx.request.body.tipid;
    let tiptext = ctx.request.body.tiptext;

    let sql = 'UPDATE growtip SET tiptext=? WHERE tipid=?'
    await ctx.db.EXCUTE(sql, [tiptext, tipid]);

    let result = await ctx.db.EXCUTE('SELECT * FROM growtip ORDER BY tipid');

    ctx.body = {
        code: 200,
        data: result
    }
})

//添加新项
router.post('/addgrowtipbackend', async (ctx, next) => {
    let cropsid = ctx.request.body.cropsid;
    let tiptext = ctx.request.body.tiptext;

    console.log(ctx.request.body);
    let sql0 = 'INSERT INTO growtip (cropsid,tiptext) VALUES (?,?)'
    await ctx.db.EXCUTE(sql0, [cropsid, tiptext]);

    ctx.body = {
        code: 200
    }
})

//删除项
router.post('/deletegrowtipbackend', async (ctx, next) => {
    let tipid = ctx.request.body.tipid;

    let sql = 'DELETE FROM growtip WHERE tipid=?'
    await ctx.db.EXCUTE(sql, [tipid]);

    let result = await ctx.db.EXCUTE('SELECT * FROM guide ORDER BY guideid');

    ctx.body = {
        code: 200,
        data: result
    }

});



module.exports = router